package com.wlyuan.open.scanner.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wlyuan.open.scanner.dao.entity.PriceOrderCacheDO;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author yuanjie
 * @since 2022-11-07
 */
public interface PriceOrderCacheMapper extends BaseMapper<PriceOrderCacheDO> {

    /**
     * 异常费用
     *
     * @param orderId
     * @return
     */
    @Select("select SUM(pdd.Detail_Amount) from Price_OrderCache poc " +
            "left join Price_DocDetails pdd on poc.Cache_DocID = pdd.Detail_DocID " +
            "where poc.Cache_OrderID = #{orderId} and poc.Cache_Type = 9 and pdd.Detail_AdditionType  = 15")
    BigDecimal getExceptionCost(Long orderId);

    /**
     * 获取合约匹配信息
     *
     * @param orderId
     * @return
     */
    @Select("select poc.Cache_ID, poc.Cache_OrderID, poc.Cache_Type, poc.Cache_IsAddition, poc.Cache_Amount, poc.Cache_Author, poc.Cache_InsertTime, poc.Cache_Updater, poc.Cache_UpdateTime, poc.Cache_Invalid, poc.Cache_Comments, poc.Cache_DocID, poc.Cache_IterationContent, poc.Cache_Desc" +
            ",pdd.Index_CreatorCompanyID as tenantId,toic.Cache_RefreshPrice as refresh from Price_OrderCache poc " +
            "left join Price_DocIndex pdd on poc.Cache_DocID = pdd.Index_ID " +
            "left join TMS_OrderIndexCaches toic on poc.Cache_OrderID = toic.Caches_OrderID " +
            "where poc.Cache_OrderID = #{orderId} and pdd.Index_Type = 2 ")
    List<PriceOrderCacheDO> getMatchInfo(Long orderId);

    @Select("select poc.Cache_ID, poc.Cache_OrderID, poc.Cache_Type, poc.Cache_IsAddition, poc.Cache_Amount, poc.Cache_Author, poc.Cache_InsertTime, poc.Cache_Updater, poc.Cache_UpdateTime, poc.Cache_Invalid, poc.Cache_Comments, poc.Cache_DocID, poc.Cache_IterationContent, poc.Cache_Desc" +
            ",toic.Cache_RefreshPrice as refresh from Price_OrderCache poc " +
            "left join TMS_OrderIndexCaches toic on poc.Cache_OrderID = toic.Caches_OrderID " +
            "where poc.Cache_OrderID = #{orderId} and poc.Cache_IsAddition = 0 and poc.Cache_Invalid = 0 " +
            "order by poc.Cache_InsertTime asc ")
    List<PriceOrderCacheDO> getNoAdditionMatchInfo(Long orderId);
}
